<template>
	<view class='container'>
		<view class="header-content">
			<img :src="baseUrl+'/static/logo.png'" alt="">
			<text>你好！欢迎登录大麦微针植发官方小程序</text>
		</view>
		<view class="content-box">
			<u-checkbox-group v-model="checkboxValue">
				<u-checkbox name="checkbox"></u-checkbox>
			</u-checkbox-group>
			<view>
				<text>我已阅读并同意协议</text>
				<text class='text-color' @click="userAgreementClick">《大麦微针植发用户协议》</text>
				<!-- <text class='text-color' @click="legalStatementClick">《法律声明》</text> -->
				<text class='text-color' @click="privacyPolicyClick">《隐私权政策》</text>
			</view>
		</view>
		<!-- <form bindsubmit="formsubmit">
			<input type="nickname" placeholder="请输入昵称" name="nickname" />
			<button form-type="submit" type="primary">提交</button>
		</form> -->
		<!-- <input class="nickname-class" type="nickname" :value="nickname" placeholder="请输入昵称" />
		-->
		<!-- <button @click="yqhyClick">邀请好友</button>
		<button @click="hbClick">落地页</button> -->
		<button v-if='checkboxValue.length>0' type="primary" open-type="getPhoneNumber" @getphonenumber="loginMethod"
			:ripple="true" class="login-button" shape="circle">登录</button>
		<button v-else type="primary" @click="loginClick" class="login-button" shape="circle">登录</button>
		<view class="tips-dm">
			<!-- {{JSON.stringify(initObj)}} -->
			新用户直接登录即可，老用户请到 个人中心 => 同步数据
		</view>
		<!-- 弹窗  -->
		<u-modal :show="show" title="知情同意书" showCancelButton>
			<view class="slot-content">
				<rich-text :nodes="agreementContent"></rich-text>
			</view>
			<view slot="confirmButton" style="display: flex;">
				<u-button :plain="true" text="取消" @click="agreementCancel"></u-button>
				<!-- <u-button :plain="true" text="确认" @click='agreementConfirm' color="#216be6"></u-button> -->
				<u-button :plain="true" text="确定" color="#216be6" open-type="getPhoneNumber"
					@getphonenumber="agreementConfirm" :ripple="true">确定</u-button>
			</view>
		</u-modal>
		<u-loading-page color="#333" :loading="loading" icon-size="80" loading-text="loading..."
			:image="baseUrl + '/static/loading.gif'" loading-mode="spinner"></u-loading-page>
		<!-- <u-loading-page color="#fff" :loading="loading" loading-mode="spinner"></u-loading-page> -->
	</view>


</template>
<script>
	import {
		Login,
		selectAuthorizedUser
	} from '../../api/axios/Login.js'
	import store from '@/store/index.js'
	import w_md5 from "@/js_sdk/zww-md5/w_md5.js"

	export default {
		data() {
			return {
				baseUrl: this.staticURL,
				loading: false,
				checkboxValue: [],
				initObj: {},
				show: false,
				agreementContent: '您是否已阅读并同意协议，《大麦微针植发用户协议》《隐私权政策》',
				customerType: '',
				registerType: ''
			}
		},
		onShow() {
			uni.hideHomeButton()
		},
		methods: {
			hbClick() {
				uni.navigateTo({
					url: '/pages-operation/poster-detail/index'
				})
			},
			yqhyClick() {
				uni.navigateTo({
					url: '/pages-operation/home/index'
				})
			},
			userAgreementClick() {
				uni.navigateTo({
					url: '/pages/UserAgreement/index'
				})

			},
			userAgreementConfirm() {},
			legalStatementClick() {},
			legalStatementConfirm() {},
			privacyPolicyClick() {
				uni.navigateTo({
					url: '/pages/PrivacyPolicy/index'
				})
			},
			privacyPolicyConfirm() {},
			nextRouter() {
				console.log('this.customerType', this.customerType);
				console.log('this.registerType', this.registerType);
				let routerObj = uni.getStorageSync('routerObj');
				if (routerObj.a) {
					switch (this.customerType) {

						case '0':
							if (this.registerType === '1') {
								uni.redirectTo({
									url: '/pages-operation/poster-detail/index'
								})
								return
							}
							// uni.showToast({
							// 	icon: "none",
							// 	title: '小程序未正式上线,尽情期待',
							// 	duration: 3000
							// });
							uni.redirectTo({
								url: '/pages-operation/home/index'
							})
							break;
						case '1':
							uni.redirectTo({
								url: '/pages-operation/poster-detail/index'
							})
							break;
						default:
							uni.redirectTo({
								url: '/pages/Main/index'
							})
							break;
					}
				} else {
					uni.redirectTo({
						url: '/pages/Main/index'
					})
				}

			},
			agreementConfirm(data) {
				this.show = false
				let _this = this
				if (!data.detail.code) {
					uni.showToast({
						icon: "none",
						title: '请授权手机号',
						duration: 2000
					});
					return false
				}
				this.loading = true
				// let appSecret = w_md5.hex_md5_32('734f63b48fded9752c5b40ce98e05519')
				wx.login({
					success(e) {
						let routerObj = uni.getStorageSync('routerObj');
						Login({
							appId: 'wx363402ec173d7c4f',
							code: e.code,
							userId: routerObj.a ? routerObj.a : null,
							inviterId: routerObj.b ? routerObj.b : null,
							// appSecret: appSecret,
							phoneCode: data.detail.code
						}).then(res => {
							store.state.infoData = res
							console.log(res);
							uni.setStorageSync('deptId', res.deptId);
							uni.setStorageSync('openId', res.openId);
							uni.setStorageSync('code', e.code);
							uni.setStorageSync('wePhone', res.wePhone);
							// res.customerType = '1' //假数据 
							uni.setStorageSync('customerType', res.customerType)
							uni.setStorageSync('registerType', res.registerType)
							_this.registerType = res.registerType;
							_this.customerType = res.customerType
							selectAuthorizedUser({
								params: {
									openId: res.openId
								}
							}).then(info => {
								_this.loading = false
								uni.setStorageSync('weUserId', info.weUserId);
								let avatarUrl = uni.getStorageSync('avatarUrl')
								let nickName = uni.getStorageSync('nickName')
								uni.setStorageSync('customerId', info.customerId);
								_this.nextRouter()
								// if (avatarUrl && nickName) {
								// 	uni.redirectTo({
								// 		url: '/pages/Main/index'
								// 	})
								// } else {
								// 	uni.redirectTo({
								// 		url: '/pages-center/WXinfo/index'
								// 	})
								// }
							})
						}).catch(err => {})
					},
					fail(e) {
						console.log('小程序调用失败');
					}
				})
			},
			agreementCancel() {
				this.show = false
			},
			loginClick() {
				if (!this.checkboxValue.length) {
					this.show = true
				} else {
					this.show = false
				}
			},
			loginMethod(data) {
				let _this = this
				if (!data.detail.code) {
					uni.showToast({
						icon: "none",
						title: '请授权手机号',
						duration: 2000
					});
					return false
				}
				this.loading = true
				// let appSecret = w_md5.hex_md5_32('734f63b48fded9752c5b40ce98e05519')
				wx.login({
					success(e) {
						let routerObj = uni.getStorageSync('routerObj');
						Login({
							appId: 'wx363402ec173d7c4f',
							code: e.code,
							userId: routerObj.a ? routerObj.a : null,
							inviterId: routerObj.b ? routerObj.b : null,
							// appSecret: appSecret,
							phoneCode: data.detail.code
						}).then(res => {
							store.state.infoData = res
							uni.setStorageSync('deptId', res.deptId);
							uni.setStorageSync('openId', res.openId);
							uni.setStorageSync('code', e.code);
							uni.setStorageSync('wePhone', res.wePhone);
							// res.customerType = '1' //假数据
							uni.setStorageSync('customerType', res.customerType)
							uni.setStorageSync('registerType', res.registerType)
							_this.registerType = res.registerType;
							_this.customerType = res.customerType
							selectAuthorizedUser({
								params: {
									openId: res.openId
								}
							}).then(info => {
								_this.loading = false
								uni.setStorageSync('weUserId', info.weUserId);
								let avatarUrl = uni.getStorageSync('avatarUrl')
								let nickName = uni.getStorageSync('nickName')
								uni.setStorageSync('customerId', info.customerId);
								_this.nextRouter()
								// if (avatarUrl && nickName) {
								// 	uni.redirectTo({
								// 		url: '/pages/Main/index'
								// 	})
								// } else {
								// 	uni.redirectTo({
								// 		url: '/pages-center/WXinfo/index'
								// 	})
								// }
							})
						}).catch(err => {})
					},
					fail(e) {
						console.log('小程序调用失败');
					}
				})
			}
		},
		async onLoad(data) {
			let openId = await uni.getStorageSync('openId')
			// data.scene = 'a=2853&b=2854'
			if (data.scene) {
				let scene = decodeURIComponent(data.scene)
				console.log(scene, 'scene');
				// 路由转json
				let arr = scene.split('&')
				let obj = {}
				arr.forEach(item => {
					obj[item.split('=')[0]] = item.split('=')[1]
				})
				uni.setStorageSync('routerObj', obj);
				let customerType = uni.getStorageSync('customerType')
				this.customerType = customerType
				return true
			} else {
				// 不存在默认0 正常注册
				this.customerType = null
			}

			if (openId) {
				this.nextRouter()
			}

		}
	}
</script>
<style lang="scss" scoped>
	.slot-content {
		font-size: 28rpx;
		color: $u-content-color;
		padding-left: 30rpx;
	}

	.container {
		padding: 60rpx;
		padding-top: 120rpx;
		box-sizing: border-box;
		min-height: 100vh;
		background-color: #fff;
	}

	.text-color {
		color: $uni-color-main;
		margin-right: 10rpx;
	}

	.header-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 100rpx;

		text {
			font-size: 32rpx;
		}

		img {
			width: 140rpx;
			height: 140rpx;
			margin-bottom: 30rpx;
			border-radius: 16rpx;
		}
	}

	.content-box {
		display: flex;
		font-size: 12px;
		color: #969696;
		width: 80%;
		margin: 0 auto;
		margin-bottom: 50px;
	}

	.login-button {
		background-color: $uni-color-main;
		color: white;
		margin-top: 10rpx;
	}

	.tips-dm {
		margin-top: 40rpx;
		font-size: 24rpx;
	}

	.nickname-class::-ms-input-placeholder {
		text-align: center;
	}

	.nickname-class::-webkit-input-placeholder {
		text-align: center;
	}
</style>